ma = 4
votefile = "test.txt"
threshStop = 0.0000000001
afile = "linkaT.txt"
hfile = "linkhT.txt"
global linkin
global linkout
#获取所有链接字典
def GetLinkDict():
    global linkin
    global linkout
    seq = [x for x in range(1, ma+1)]
    linkin = dict.fromkeys(seq)
    linkout = dict.fromkeys(seq)
    fopen = open(votefile)
    for line in fopen:
        if line[0] == '#':
            continue
        edge = line.split()
        x, y = [int(x) for x in edge]
        if linkout[x] is None:
            linkout[x] = []
        if linkin[y] is None:
            linkin[y] = []
        linkout[x].append(y)
        linkin[y].append(x)
#初始化auth，hub为1
def InitAH():
    fa = open(afile, 'w')
    fh = open(hfile, 'w')
    i = 1
    while i <= ma:
        fa.write(str(1) + ' ')
        fh.write(str(1) + ' ')
        i += 1
#计算auth ，hub值
def GetHits():
    suma = [0 for i in range(1, ma + 1)]
    sumh = [0 for i in range(1, ma + 1)]
    sumA = 0
    sumH = 0
    fa = open(afile, 'r')
    fh = open(hfile, 'r')
    dataa = fa.read()
    dataa = dataa.split()
    datah = fh.read()
    datah = datah.split()

    for item in range(1, ma+1):
        outlink = linkout[item]
        inlink = linkin[item]

        if outlink is not None:
            for j in outlink:
                sumh[item-1] += float(dataa[j-1])
                sumH += float(dataa[j-1])
        if inlink is not None:
            for j in inlink:
                suma[item - 1] += float(datah[j-1])
                sumA += float(dataa[j - 1])

    fa = open(afile, 'w')
    fh = open(hfile, 'w')
    for item in range(0, ma):
        ta = suma[item]/sumA
        th = sumh[item] / sumH
        fa.write(str(ta) + ' ')
        fh.write(str(th) + ' ')
GetLinkDict()
InitAH()
GetHits()

# Hub
# Auth